﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using DTO;
using DAO;
using BUS;

namespace Cucre
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        private PROVIDER provider;

        protected PROVIDER Provider
        {
            get { return provider; }
            set { provider = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string path = Server.MapPath("theme.txt");
            StreamReader SR = new StreamReader(path);
            string nameTheme = SR.ReadLine();
            SR.Close();
            base.OnPreInit(e);
            if (Session["MyTheme"] == null)
            {
                Session.Add("MyTheme", nameTheme);
                Page.Theme = (string)Session["MyTheme"];
            }
            else
            {
                Page.Theme = (string)Session["MyTheme"];
            }
            //   Session.Add("user_login", null);
        }

        protected void Registerbt_Click(object sender, EventArgs e)
        {
            if (Company_Name.Text.Trim() == null || Company_Name.Text.Trim() == "")
            {
                ErrorCompany.Visible = true;
                Company_Name.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorCompany.Visible = false;
                Company_Name.BorderColor = System.Drawing.Color.Black;
            }
            if (User_Name.Text.Trim() == null || User_Name.Text.Trim() == "")
            {
                ErrorUsername.Visible = true;
                User_Name.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorUsername.Visible = false;
                User_Name.BorderColor = System.Drawing.Color.Black;
            }
            if (Password.Text.Trim() == null || Password.Text.Trim() == "")
            {
                ErrorPass.Visible = true;
                Password.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorPass.Visible = false;
                Password.BorderColor = System.Drawing.Color.Black;
            }
            if (Confirm_Password.Text.Trim() == null || Confirm_Password.Text.Trim() == "")
            {
                ErrorConfirmPass.Visible = true;
                Confirm_Password.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorConfirmPass.Visible = false;
                Confirm_Password.BorderColor = System.Drawing.Color.Black;
            }
            if (Email_Address.Text.Trim() == null || Email_Address.Text.Trim() == "")
            {
                ErrorEmail.Visible = true;
                Email_Address.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorEmail.Visible = false;
                Email_Address.BorderColor = System.Drawing.Color.Black;
            }
            if (Address.Text.Trim() == null || Address.Text.Trim() == "")
            {
                ErrorAddress.Visible = true;
                Address.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorAddress.Visible = false;
                Address.BorderColor = System.Drawing.Color.Black;
            }
            if (phonetb.Text.Trim() == null || phonetb.Text.Trim() == "")
            {
                ErrorPhone.Visible = true;
                phonetb.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorPhone.Visible = false;
                phonetb.BorderColor = System.Drawing.Color.Black;
            }
            if (cmnd.Text.Trim() == null || cmnd.Text.Trim() == "")
            {
                ErrorCMND.Visible = true;
                cmnd.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorCMND.Visible = false;
                cmnd.BorderColor = System.Drawing.Color.Black;
            }
            if (fullname.Text.Trim() == null || fullname.Text.Trim() == "")
            {
                errorFullname.Visible = true;
                fullname.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                errorFullname.Visible = false;
                fullname.BorderColor = System.Drawing.Color.Black;
            }
            if (capchar.Text.Trim() == null || capchar.Text.Trim() == "")
            {
                ErrorCapchar.Visible = true;
                capchar.BorderColor = System.Drawing.Color.Red;
                return;
            }
            else
            {
                ErrorCapchar.Visible = false;
                capchar.BorderColor = System.Drawing.Color.Black;
            }
            if (!Accept_CheckBox.Checked)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Bạn chưa đồng ý gia nhập cucre.vn')", true);
            }
            else
            {
                ccJoin.ValidateCaptcha(capchar.Text);
                if (!ccJoin.UserValidated)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Mã bảo vệ không đúng')", true);
                }
                else
                {
                    ErrorCompany.Visible = false;
                    Company_Name.BorderColor = System.Drawing.Color.Black;
                    ErrorUsername.Visible = false;
                    User_Name.BorderColor = System.Drawing.Color.Black;
                    ErrorPass.Visible = false;
                    Password.BorderColor = System.Drawing.Color.Black;
                    ErrorConfirmPass.Visible = false;
                    Confirm_Password.BorderColor = System.Drawing.Color.Black;
                    ErrorEmail.Visible = false;
                    Email_Address.BorderColor = System.Drawing.Color.Black;
                    ErrorAddress.Visible = false;
                    Address.BorderColor = System.Drawing.Color.Black;
                    ErrorPhone.Visible = false;
                    phonetb.BorderColor = System.Drawing.Color.Black;
                    ErrorCMND.Visible = false;
                    cmnd.BorderColor = System.Drawing.Color.Black;
                    errorFullname.Visible = false;
                    fullname.BorderColor = System.Drawing.Color.Black;

                    provider = new PROVIDER();
                    provider.ACCOUNT = new ACCOUNT();
                    provider.ACCOUNT.ACCOUNT_STYLE = false;
                    provider.ACCOUNT.CMND = cmnd.Text.Trim();
                    provider.ACCOUNT.FULLNAME = fullname.Text.Trim();
                    provider.ACCOUNT.VISIBLE = true;
                    provider.COMPANY_NAME = Company_Name.Text.Trim();
                    provider.ACCOUNT.USERNAME = User_Name.Text.Trim();
                    provider.ACCOUNT.PASSWORD = Password.Text.Trim();
                    provider.ACCOUNT.EMAIL = Email_Address.Text.Trim();
                    provider.ACCOUNT.ADDRESS = Address.Text.Trim();
                    provider.ACCOUNT.PHONE_NUMBER = phonetb.Text.Trim();
                    provider.ACCOUNT.CMND = cmnd.Text.Trim();
                    provider.INTRODUCE_COMPANY = Introduce_Company.Text.Trim();

                    bool Check = ProviderBUS.Insert(provider);
                    if (Check)
                    {
                        Session.Add("user_login", provider);
                        Session.Add("user_style", "4");
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Bạn đã đăng kí thành công');window.location.href='index.aspx'", true);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Đăng kí thất bại. Bạn vui lòng kiểm tra lại", true);

                    }
                }
            }
        }

    }
}
